<!DOCTYPE html>

<html lang="en" data-content_root="../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <title>FindEnvModules &mdash; CMake 4.1.1 Documentation</title>

    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=a2c47e09" />
    <link rel="stylesheet" type="text/css" href="../_static/cmake.css?v=4d06bd55" />
    
    <script src="../_static/documentation_options.js?v=e6a937a4"></script>
    <script src="../_static/doctools.js?v=9bcbadda"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    
    <link rel="icon" href="../_static/cmake-favicon.ico"/>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="FindEXPAT" href="FindEXPAT.html" />
    <link rel="prev" title="FindDoxygen" href="FindDoxygen.html" />
 

  </head><body>
    <input id="sidebar-check" type="checkbox" />
    <label id="sidebar-overlay" for="sidebar-check"></label>



    <div class="related relbar1" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="FindEXPAT.html" title="FindEXPAT"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="FindDoxygen.html" title="FindDoxygen"
             accesskey="P">previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" accesskey="U">cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">FindEnvModules</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="findenvmodules">
<span id="module:FindEnvModules"></span><h1>FindEnvModules<a class="headerlink" href="#findenvmodules" title="Link to this heading">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
<p>Finds an Environment Modules implementation and provides commands for use in
CMake scripts:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">find_package(</span><span class="nb">EnvModules</span><span class="w"> </span><span class="p">[...]</span><span class="nf">)</span>
</pre></div>
</div>
<p>The Environment Modules system is a command-line tool that manages Unix-like
shell environments by dynamically modifying environment variables.
It is commonly used in High-Performance Computing (HPC) environments to
support multiple software versions or configurations.</p>
<p>This module is compatible with the two most common implementations:</p>
<ul class="simple">
<li><p>Lua-based Lmod</p></li>
<li><p>TCL-based Environment Modules</p></li>
</ul>
<p>This module is primarily intended for setting up compiler and library
environments within a <a class="reference internal" href="../manual/ctest.1.html#ctest-script"><span class="std std-ref">CTest Script</span></a> (<code class="docutils literal notranslate"><span class="pre">ctest</span> <span class="pre">-S</span></code>).
It may also be used in a <a class="reference internal" href="../manual/cmake.1.html#script-processing-mode"><span class="std std-ref">CMake Script</span></a>
(<code class="docutils literal notranslate"><span class="pre">cmake</span> <span class="pre">-P</span></code>).</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The loaded environment will not persist beyond the end of the calling
process.  Do not use this module in CMake project code (such as
<code class="docutils literal notranslate"><span class="pre">CMakeLists.txt</span></code>) to load compiler environments, as the environment
changes will not be available during the build phase.  In such a case, load
the desired environment before invoking CMake or the generated build system.</p>
</div>
<section id="result-variables">
<h2>Result Variables<a class="headerlink" href="#result-variables" title="Link to this heading">¶</a></h2>
<p>This module defines the following variables:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">EnvModules_FOUND</span></code></dt><dd><p>Boolean indicating whether a compatible Environment Modules framework is
found.</p>
</dd>
</dl>
</section>
<section id="cache-variables">
<h2>Cache Variables<a class="headerlink" href="#cache-variables" title="Link to this heading">¶</a></h2>
<p>The following cache variables may also be set:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">EnvModules_COMMAND</span></code></dt><dd><p>The path to a low level module command to use.</p>
</dd>
</dl>
</section>
<section id="hints">
<h2>Hints<a class="headerlink" href="#hints" title="Link to this heading">¶</a></h2>
<p>This module accepts the following variables before calling the
<code class="docutils literal notranslate"><span class="pre">find_package(EnvModules)</span></code>:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">ENV{MODULESHOME}</span></code></dt><dd><p>This environment variable is usually set by the Environment Modules
implementation, and can be used as a hint to locate the module command to
execute.</p>
</dd>
</dl>
</section>
<section id="commands">
<h2>Commands<a class="headerlink" href="#commands" title="Link to this heading">¶</a></h2>
<p>This module provides the following commands for interacting with the
Environment Modules system, if found:</p>
<dl class="cmake command">
<dt class="sig sig-object cmake" id="command:env_module">
<span class="sig-name descname"><span class="pre">env_module</span></span><a class="headerlink" href="#command:env_module" title="Link to this definition">¶</a></dt>
<dd><p>Executes an arbitrary module command:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">env_module(</span><span class="nv">&lt;command&gt;</span><span class="w"> </span><span class="nv">&lt;args&gt;...</span><span class="nf">)</span>
<span class="nf">env_module(</span>
<span class="w">  </span><span class="no">COMMAND</span><span class="w"> </span><span class="nv">&lt;command&gt;</span><span class="w"> </span><span class="nv">&lt;args&gt;...</span>
<span class="w">  </span><span class="p">[</span><span class="no">OUTPUT_VARIABLE</span><span class="w"> </span><span class="nv">&lt;out-var&gt;</span><span class="p">]</span>
<span class="w">  </span><span class="p">[</span><span class="no">RESULT_VARIABLE</span><span class="w"> </span><span class="nv">&lt;ret-var&gt;</span><span class="p">]</span>
<span class="nf">)</span>
</pre></div>
</div>
<p>The options are:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">COMMAND</span> <span class="pre">&lt;command&gt;</span> <span class="pre">&lt;args&gt;...</span></code></dt><dd><p>The module sub-command and arguments to execute as if passed directly to
the module command in the shell environment.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">OUTPUT_VARIABLE</span> <span class="pre">&lt;out-var&gt;</span></code></dt><dd><p>Stores the standard output of the executed module command in the specified
variable.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">RESULT_VARIABLE</span> <span class="pre">&lt;ret-var&gt;</span></code></dt><dd><p>Stores the return code of the executed module command in the specified
variable.</p>
</dd>
</dl>
</dd></dl>

<dl class="cmake command">
<dt class="sig sig-object cmake" id="command:env_module_swap">
<span class="sig-name descname"><span class="pre">env_module_swap</span></span><a class="headerlink" href="#command:env_module_swap" title="Link to this definition">¶</a></dt>
<dd><p>Swaps one module for another:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">env_module_swap(</span>
<span class="w">  </span><span class="nv">&lt;out-mod&gt;</span>
<span class="w">  </span><span class="nv">&lt;in-mod&gt;</span>
<span class="w">  </span><span class="p">[</span><span class="no">OUTPUT_VARIABLE</span><span class="w"> </span><span class="nv">&lt;out-var&gt;</span><span class="p">]</span>
<span class="w">  </span><span class="p">[</span><span class="no">RESULT_VARIABLE</span><span class="w"> </span><span class="nv">&lt;ret-var&gt;</span><span class="p">]</span>
<span class="nf">)</span>
</pre></div>
</div>
<p>This is functionally equivalent to the <code class="docutils literal notranslate"><span class="pre">module</span> <span class="pre">swap</span> <span class="pre">&lt;out-mod&gt;</span> <span class="pre">&lt;in-mod&gt;</span></code>
shell command.</p>
<p>The options are:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">OUTPUT_VARIABLE</span> <span class="pre">&lt;out-var&gt;</span></code></dt><dd><p>Stores the standard output of the executed module command in the specified
variable.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">RESULT_VARIABLE</span> <span class="pre">&lt;ret-var&gt;</span></code></dt><dd><p>Stores the return code of the executed module command in the specified
variable.</p>
</dd>
</dl>
</dd></dl>

<dl class="cmake command">
<dt class="sig sig-object cmake" id="command:env_module_list">
<span class="sig-name descname"><span class="pre">env_module_list</span></span><a class="headerlink" href="#command:env_module_list" title="Link to this definition">¶</a></dt>
<dd><p>Retrieves the list of currently loaded modules:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">env_module_list(</span><span class="nv">&lt;out-var&gt;</span><span class="nf">)</span>
</pre></div>
</div>
<p>This is functionally equivalent to the <code class="docutils literal notranslate"><span class="pre">module</span> <span class="pre">list</span></code> shell command.
The result is stored in <code class="docutils literal notranslate"><span class="pre">&lt;out-var&gt;</span></code> as a properly formatted CMake
<a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> variable.</p>
</dd></dl>

<dl class="cmake command">
<dt class="sig sig-object cmake" id="command:env_module_avail">
<span class="sig-name descname"><span class="pre">env_module_avail</span></span><a class="headerlink" href="#command:env_module_avail" title="Link to this definition">¶</a></dt>
<dd><p>Retrieves the list of available modules:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">env_module_avail(</span><span class="p">[</span><span class="nv">&lt;mod-prefix&gt;</span><span class="p">]</span><span class="w"> </span><span class="nv">&lt;out-var&gt;</span><span class="nf">)</span>
</pre></div>
</div>
<p>This is functionally equivalent to the <code class="docutils literal notranslate"><span class="pre">module</span> <span class="pre">avail</span> <span class="pre">&lt;mod-prefix&gt;</span></code> shell
command.  The result is stored in <code class="docutils literal notranslate"><span class="pre">&lt;out-var&gt;</span></code> as a properly formatted
CMake <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> variable.</p>
</dd></dl>

</section>
<section id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
<p>In the following example, this module is used in a CTest script to configure
the compiler and libraries for a Cray Programming Environment.
After the Environment Modules system is found, the <code class="docutils literal notranslate"><span class="pre">env_module()</span></code> command is
used to load the necessary compiler, MPI, and scientific libraries to set up
the build environment.  The <code class="docutils literal notranslate"><span class="pre">CRAYPE_LINK_TYPE</span></code> environment variable is set
to <code class="docutils literal notranslate"><span class="pre">dynamic</span></code> to specify dynamic linking.  This instructs the Cray Linux
Environment compiler driver to link against dynamic libraries at runtime,
rather than linking static libraries at compile time.  As a result, the
compiler produces dynamically linked executable files.</p>
<div class="literal-block-wrapper docutils container" id="id1">
<div class="code-block-caption"><span class="caption-text"><code class="docutils literal notranslate"><span class="pre">example-script.cmake</span></code></span><a class="headerlink" href="#id1" title="Link to this code">¶</a></div>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">set(</span><span class="no">CTEST_BUILD_NAME</span><span class="w"> </span><span class="s">&quot;CrayLinux-CrayPE-Cray-dynamic&quot;</span><span class="nf">)</span>
<span class="nf">set(</span><span class="no">CTEST_BUILD_CONFIGURATION</span><span class="w"> </span><span class="nb">Release</span><span class="nf">)</span>
<span class="nf">set(</span><span class="no">CTEST_BUILD_FLAGS</span><span class="w"> </span><span class="s">&quot;-k -j8&quot;</span><span class="nf">)</span>
<span class="nf">set(</span><span class="no">CTEST_CMAKE_GENERATOR</span><span class="w"> </span><span class="s">&quot;Unix Makefiles&quot;</span><span class="nf">)</span>

<span class="c"># ...</span>

<span class="nf">find_package(</span><span class="nb">EnvModules</span><span class="w"> </span><span class="no">REQUIRED</span><span class="nf">)</span>

<span class="c"># Clear all currently loaded Environment Modules to start with a clean state</span>
<span class="nf">env_module(</span><span class="nb">purge</span><span class="nf">)</span>

<span class="c"># Load the base module-handling system to use other modules</span>
<span class="nf">env_module(</span><span class="nb">load</span><span class="w"> </span><span class="nb">modules</span><span class="nf">)</span>

<span class="c"># Load Cray Programming Environment (Cray PE) support, which manages</span>
<span class="c"># platform-specific optimizations and architecture selection</span>
<span class="nf">env_module(</span><span class="nb">load</span><span class="w"> </span><span class="nb">craype</span><span class="nf">)</span>

<span class="c"># Load the Cray programming environment</span>
<span class="nf">env_module(</span><span class="nb">load</span><span class="w"> </span><span class="nb">PrgEnv-cray</span><span class="nf">)</span>

<span class="c"># Load settings targeting the Intel Knights Landing (KNL) CPU architecture</span>
<span class="nf">env_module(</span><span class="nb">load</span><span class="w"> </span><span class="nb">craype-knl</span><span class="nf">)</span>

<span class="c"># Load the Cray MPI (Message Passing Interface) library, needed for</span>
<span class="c"># distributed computing</span>
<span class="nf">env_module(</span><span class="nb">load</span><span class="w"> </span><span class="nb">cray-mpich</span><span class="nf">)</span>

<span class="c"># Load Cray&#39;s scientific library package, which includes optimized math</span>
<span class="c"># libraries (like BLAS, LAPACK)</span>
<span class="nf">env_module(</span><span class="nb">load</span><span class="w"> </span><span class="nb">cray-libsci</span><span class="nf">)</span>

<span class="nf">set(</span><span class="o">ENV{</span><span class="nt">CRAYPE_LINK_TYPE</span><span class="o">}</span><span class="w"> </span><span class="nb">dynamic</span><span class="nf">)</span>

<span class="c"># ...</span>
</pre></div>
</div>
</div>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
  <div>
    <h3>Table of Contents</h3>
    <ul>
<li><a class="reference internal" href="#">FindEnvModules</a><ul>
<li><a class="reference internal" href="#result-variables">Result Variables</a></li>
<li><a class="reference internal" href="#cache-variables">Cache Variables</a></li>
<li><a class="reference internal" href="#hints">Hints</a></li>
<li><a class="reference internal" href="#commands">Commands</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
</ul>

  </div>
  <div>
    <h4>Previous topic</h4>
    <p class="topless"><a href="FindDoxygen.html"
                          title="previous chapter">FindDoxygen</a></p>
  </div>
  <div>
    <h4>Next topic</h4>
    <p class="topless"><a href="FindEXPAT.html"
                          title="next chapter">FindEXPAT</a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/module/FindEnvModules.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="FindEXPAT.html" title="FindEXPAT"
             >next</a> |</li>
        <li class="right" >
          <a href="FindDoxygen.html" title="FindDoxygen"
             >previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" >cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">FindEnvModules</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
    &#169; Copyright 2000-2025 Kitware, Inc. and Contributors.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3.
    </div>
<script type="text/javascript">
(function() {
  "use strict";
  const hide = () => document.getElementById("sidebar-check").checked = false;
  addEventListener("keydown", e => (e.key === "Escape") && hide());
  addEventListener("click", e => (e.target.tagName === "A") && hide());
  addEventListener("hashchange", hide)
})();
</script>
  </body>
</html>